//
//  UIButton_Extesion.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/11/23.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "UIButton_Extesion.h"

@implementation UIButton_Extesion


+(UIButton *)factoryLabel:(NSString *)title titleColor:(UIColor *)titleColor backImageName:(NSString *)backImageName imageName:(NSString *)imageName fontSize:(CGFloat)fontSize;{
    
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    if(backImageName != nil){
        [button setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
        //    原文链接：http://www.jianshu.com/p/996c27c013c5
        [button setBackgroundImage:[UIImage imageNamed:[backImageName stringByAppendingString:@"_highlighted"]] forState:UIControlStateHighlighted];
    }
    if(imageName != nil){
        
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]] forState:UIControlStateHighlighted];
    }
    
    [button sizeToFit];
    return button;
    
}


-(id)initWithTitle:(NSString *)title titleColor:(UIColor *)titleColor backImageName:(NSString *)backImageName imageName:(NSString *)imageName fontSize:(CGFloat)fontSize;{
    
    self = [super init];
    if (self) {
        //        UIButton *button = [[UIButton alloc]init];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:titleColor forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        if(backImageName != nil){
            [self setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
            [self setBackgroundImage:[UIImage imageNamed:[backImageName stringByAppendingString:@"_highlighted"]] forState:UIControlStateHighlighted];
        }
        if(imageName != nil){
            [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            [self setImage:[UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]] forState:UIControlStateHighlighted];
        }
    }
    
    [self sizeToFit];
    return self;
}

-(id)initWithBackImageName:(NSString *)backImageName imageName:(NSString *)imageName;{
    self = [super init];
    if (self) {
        if(backImageName != nil){
            [self setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
            [self setBackgroundImage:[UIImage imageNamed:[backImageName stringByAppendingString:@"_highlighted"]] forState:UIControlStateHighlighted];
        }
        if(imageName != nil){
            [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            [self setImage:[UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]] forState:UIControlStateHighlighted];
        }
    }
    [self sizeToFit];
    return self;
}

@end
